package cn.gene.timeyan.article.webapi.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

@Component("myAdaptableJobFactory")
public class MyAdaptableJobFactory extends AdaptableJobFactory {
    // 可以将一个对象添加到spring的IOC容器中,并完成该对象的属性注入
    @Autowired
    private AutowireCapableBeanFactory autowireCapableBeanFactory;

    /**
     * 该方法将实例化的对象,手动添加到spring的容器,并完成对象的注入
     *
     * @param bundle
     * @return
     * @throws Exception
     */
    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        // 调用父类方法创建实例
        Object object = super.createJobInstance(bundle);
        // 将该实例交由spring管理
        autowireCapableBeanFactory.autowireBean(object);
        return object;
    }
}